home *** CD-ROM | disk | FTP | other *** search
/ Internet Surfer: Getting Started / Internet Surfer - Getting Started (Wayzata Technology)(7231)(1995).bin / pc / mac / bonus / peter_le / dehqx-20 / dehqx.p < prev    next >
Text File  |  1991-08-23  |  9KB  |  358 lines

  1. unit DeHQX;
  2. { DeHQX v2.0.0 ⌐ Peter Lewis, Aug 1991 }
  3.  
  4. interface
  5.  
  6.     uses
  7.         MyTypes, MyFileSystem, MyStandardFile, AppGlobals, CRCs, MyUtilities, MyMainLoop, {}
  8.         SmallEvents, MyNotifier, MyMenus, Displays, HQXLists, ReadHQX, DisplayHQX, {}
  9.         MySystem7, Preferences;
  10.  
  11.     procedure DeHQXFiles;
  12.     procedure DeHQXList;
  13.     procedure DeHQXParameters;
  14.     procedure AddFolder (vrn: integer; dirID: longInt);
  15.  
  16. implementation
  17.  
  18.     const
  19.         update_period = 1024;
  20.         display_updates = 1024 div update_period;
  21.  
  22.     function DoFork (vrn: integer; dirID: longInt; var name: str63; {}
  23.                                     wp: windowPtr; fork: forkType; len: longInt): OSErr;
  24.         type
  25.             updateRange = 0..update_period;
  26.         var
  27.             oe, ooe: OSErr;
  28.             thecrc, actcrc: integer;
  29.             i: longInt;
  30.             j: integer;
  31.             b: byte;
  32.             outfile: integer;
  33.             buffer: packed array[updateRange] of byte;
  34.             bptr: updateRange;
  35.             blen: longInt;
  36.         procedure SimpleHandleUpdateEvents;
  37.             var
  38.                 reply: HEReply;
  39.         begin
  40.             HandleCancelErrorEvents(0, nil, oe, reply);
  41.             if reply.todo = T_Update then begin
  42.                 BeginUpdate(wp);
  43.                 DisplayUpdate(wp);
  44.                 EndUpdate(wp);
  45.             end;
  46.         end;
  47.     begin
  48.         crc := 0;
  49.         if fork = data_fork then
  50.             oe := MFSOpenDF(outfile, vrn, dirID, name, POut)
  51.         else
  52.             oe := MFSOpenRF(outfile, vrn, dirID, name, POut);
  53.         DisplayFork(wp, fork, 0, oe);
  54.         SimpleHandleUpdateEvents;
  55.         if oe = noErr then begin
  56.             for i := 1 to len div update_period do begin
  57.                 bptr := 0;
  58.                 for j := 1 to update_period do begin
  59.                     oe := ReadByte(b);
  60.                     if oe <> noErr then
  61.                         leave;
  62.                     buffer[bptr] := b;
  63.                     bptr := bptr + 1;
  64.                 end;
  65.                 if oe = noErr then begin
  66.                     blen := bptr;
  67.                     oe := FSWrite(outfile, blen, @buffer);
  68.                 end;
  69.                 if oe <> noErr then
  70.                     leave;
  71.                 SimpleHandleUpdateEvents;
  72.                 if oe <> noErr then
  73.                     leave;
  74.                 if i mod display_updates = 0 then
  75.                     DisplayFork(wp, fork, i * update_period, oe);
  76.             end;
  77.             if oe = noErr then begin
  78.                 bptr := 0;
  79.                 for j := 1 to len mod update_period do begin
  80.                     oe := ReadByte(b);
  81.                     if oe <> noErr then
  82.                         leave;
  83.                     buffer[bptr] := b;
  84.                     bptr := bptr + 1;
  85.                 end;
  86.                 if oe = noErr then begin
  87.                     blen := bptr;
  88.                     oe := FSWrite(outfile, blen, @buffer);
  89.                 end;
  90.             end;
  91.             CalcCRC(crc, 0);
  92.             CalcCRC(crc, 0);
  93.             actcrc := crc;
  94.             if oe = noErr then
  95.                 oe := ReadInteger(thecrc);
  96.             if (actcrc <> thecrc) and (oe = noErr) then
  97.                 oe := HqxFormatErr;
  98.             DisplayFork(wp, fork, len, oe);
  99.             ooe := FSClose(outfile);
  100.         end;
  101.         DoFork := oe;
  102.     end;
  103.  
  104.     procedure DeHQXList;
  105.         const
  106.             fin_err = 1;
  107.         var
  108.             reply: MySFReply;
  109.             oe, ooe: integer;
  110.             hi: hqxInfo;
  111.             wp: windowPtr;
  112.             any_saved, any_errors, first_save: boolean;
  113.             alertID, alertButton: integer;
  114.             dummy_name: str255;
  115.             prompting: promptStates;
  116.             savefolder: boolean;
  117.             ovrn: integer;
  118.             odirID: longInt;
  119.             did_something: boolean;
  120.             fdel, fstop: boolean;
  121.         procedure SetFirstSave (vrn: integer; dirID: longInt);
  122.         begin
  123.             if first_save then begin
  124.                 SetSFFile(vrn, dirID);
  125.                 first_save := false;
  126.             end;
  127.         end;
  128.         procedure Interact;
  129.             var
  130.                 oe: OSErr;
  131.         begin
  132.             oe := MyInteractWithUser(nil);
  133.         end;
  134.     begin
  135.         if AnyInputFiles then begin
  136.             OpenDisplay(wp);
  137.             first_save := true;
  138.             prompting := prefs.prompt_state;
  139.             oe := noErr;
  140.             while AnyInputFiles and (oe <> cancelErr) do begin
  141.                 did_something := true;
  142.                 StartList;
  143.                 savefolder := false;
  144.                 any_saved := false;
  145.                 any_errors := false;
  146.                 oe := OpenHQX;
  147.                 while oe = noErr do begin
  148.                     oe := ReadHeader(hi, wp);
  149.                     if oe = noErr then begin
  150.                         with reply do begin
  151.                             RfName := hi.name;
  152.                             Rgood := true;
  153.                             if savefolder then begin
  154.                                 RvRefNum := ovrn;
  155.                                 RdirID := odirID;
  156.                             end
  157.                             else
  158.                                 CreateFolder(RvRefNum, RdirID);
  159.                             case prompting of
  160.                                 PS_Always: 
  161.                                     begin
  162.                                     Interact;
  163.                                     SetFirstSave(RvRefNum, RdirID);
  164.                                     PutFolder(GetGlobalString(sfput_string), RfName, put_folder_id, reply);
  165.                                     if Rgood and Rfolder then begin
  166.                                         if RfName = '' then begin
  167.                                             RfName := hi.name;
  168.                                             MFSUniqueName(RvRefNum, RdirID, RfName);  { cant really put up another dialog box! }
  169.                                         end;
  170.                                         savefolder := true;
  171.                                         prompting := PS_Exists;
  172.                                         ovrn := RvRefNum;
  173.                                         odirID := RdirID;
  174.                                     end;
  175.                                 end;
  176.                                 PS_Exists: 
  177.                                     if MFSExists(RvRefNum, RdirID, RfName) then begin
  178.                                         Interact;
  179.                                         SetSFFile(RvRefNum, RdirID);
  180.                                         PutFolder(GetGlobalString(sfput_string), RfName, put_folder_id, reply);
  181.                                         if Rgood then begin
  182.                                             if Rfolder then begin
  183.                                                 savefolder := true;
  184.                                                 ovrn := RvRefNum;
  185.                                                 odirID := RdirID;
  186.                                             end
  187.                                             else begin
  188.                                                 savefolder := false;
  189.                                                 prompting := prefs.prompt_state;
  190.                                             end;
  191.                                         end;
  192.                                     end;
  193.                                 PS_Skip: 
  194.                                     Rgood := not MFSExists(RvRefNum, RdirID, RfName);
  195.                                 PS_Overwrite: 
  196.                                     ;
  197.                                 PS_Unique: 
  198.                                     MFSUniqueName(RvRefNum, RdirID, RfName);
  199.                             end; {case}
  200.                             if not Rgood then
  201.                                 cycle;
  202.                             with hi do begin
  203.                                 name := RfName;
  204.                                 wdrn := RvRefNum;
  205.                                 dirID := RdirID;
  206.                                 DisplayOpen(wp, hi);
  207.                                 if oe = noErr then
  208.                                     oe := MFSCreate(RvRefNum, RdirID, RfName, c, t);
  209.                                 if oe = noErr then
  210.                                     oe := DoFork(RvRefNum, RdirID, RfName, wp, data_fork, dlen);
  211.                                 if oe = noErr then
  212.                                     oe := DoFork(RvRefNum, RdirID, RfName, wp, rsrc_fork, rlen);
  213.                                 if oe = noErr then begin
  214.                                     oe := ReadColon;
  215.                                 end;
  216.                                 DisplayFinish(wp, oe);
  217.                             end; {with}
  218.                             if oe = noErr then begin
  219.                                 any_saved := true;
  220.                             end
  221.                             else begin
  222.                                 ParamText(RfName, '', '', '');
  223.                                 any_errors := true;
  224.                                 case oe of
  225.                                     cancelErr: 
  226.                                         begin
  227.                                         fdel := true;
  228.                                         fstop := true;
  229.                                     end;
  230.                                     HqxFormatErr: 
  231.                                         begin
  232.                                         Interact;
  233.                                         alertButton := Alert(hqx_error_alert_id, nil);
  234.                                         fstop := not odd(alertButton);
  235.                                         fdel := alertButton < 3;
  236.                                     end;
  237.                                     otherwise
  238.                                         begin
  239.                                         Interact;
  240.                                         alertButton := Alert(disk_error_alert_id, nil);
  241.                                         fstop := alertButton = 1;
  242.                                         fdel := true;
  243.                                     end;
  244.                                 end;
  245.                                 if fstop then
  246.                                     oe := cancelErr
  247.                                 else begin
  248.                                     oe := noErr;
  249.                                     quitNow := false;
  250.                                     HiliteMenu(0);
  251.                                 end;
  252.                                 if fdel then
  253.                                     ooe := MFSDelete(RvRefNum, RdirID, RfName);
  254.                             end;
  255.                             DisplayClose(wp);
  256.                         end; {with}
  257.                     end; {if}
  258.                 end;
  259.                 FinishHQX;
  260.                 FinishList(prefs.delete_state and any_saved and not any_errors);
  261.             end;
  262.             if did_something then begin
  263.                 if prefs.auto_quit_state and any_saved and not any_errors then
  264.                     quitNow := true;
  265.                 if (oe <> cancelErr) or not in_foreground then begin  { No sense beeping and notifying if the user canceled! }
  266.                     if prefs.beep_state then
  267.                         SysBeep(3);
  268.                     if prefs.notify_state and not in_foreground then
  269.                         Notify(true, false, 128, 1, 0, 0);
  270.                 end;
  271.             end;
  272.             CloseDisplay(wp);
  273.             size_in_lists := 0;
  274.             size_processed := 0;
  275.         end;
  276.     end;
  277.  
  278.     function HQXHook (var pb: HParamBlockRec): boolean;
  279.     begin
  280.         case prefs.display_state of
  281.             DS_All: 
  282.                 HQXHook := false;
  283.             DS_TEXT: 
  284.                 HQXHook := pb.ioFlFndrInfo.fdType <> 'TEXT';
  285.             DS_HQX: 
  286.                 HQXHook := (pb.ioFlFndrInfo.fdType <> 'TEXT') or not EqualString(Copy(pb.ioNamePtr^, length(pb.ioNamePtr^) - 2, 3), 'hqx', false, false);
  287.         end;
  288.     end;
  289.  
  290.     procedure AddFolder (vrn: integer; dirID: longInt);
  291.         var
  292.             pb: HParamBlockRec;
  293.             name: str255;
  294.             i: integer;
  295.             oe: OSErr;
  296.     begin
  297.         i := 1;
  298.         with pb do
  299.             repeat
  300.                 name := '';
  301.                 ioNamePtr := @name;
  302.                 ioVRefNum := vrn;
  303.                 ioDirID := dirID;
  304.                 ioFVersNum := 0;
  305.                 ioFDirIndex := i;
  306.                 i := i + 1;
  307.                 oe := PBHGetFInfo(@pb, false);
  308.                 if oe = noErr then
  309.                     if not HQXHook(pb) then begin
  310.                         AddFile(vrn, dirID, name, prefs.create_dir_state <> CDS_Never, pb.ioFlLgLen);
  311.                     end;
  312.             until oe <> noErr;
  313.     end;
  314.  
  315.     procedure DeHQXFiles;
  316.         var
  317.             typeList: SFTypeList;
  318.             reply: MySFReply;
  319.             ovrn: integer;
  320.             dirID: longInt;
  321.             oe: OSErr;
  322.     begin
  323.         GetFolder(@HqxHook, -1, typeList, get_folder_id, reply);
  324.         HiliteMenu(0);
  325.         with reply do
  326.             if Rgood then begin
  327.                 if not Rfolder then begin
  328.                     AddFile(RvRefNum, RdirID, RfName, prefs.create_dir_state = CDS_Always, -1);
  329.                 end
  330.                 else begin
  331.                     AddFolder(RvRefNum, RdirID);
  332.                 end;
  333.             end;
  334.     end;
  335.  
  336.     procedure DeHQXParameters;
  337.         var
  338.             paramCount, paramMessage, i: integer;
  339.             tf: appFile;
  340.             pb: paramBlockRec;
  341.             ovrn: integer;
  342.             odirID, dirID: longInt;
  343.             oe: OSErr;
  344.             sh: stringHandle;
  345.     begin
  346.         CountAppFiles(paramMessage, paramCount);
  347.         GetAppFiles(1, tf);
  348.         for i := 1 to paramCount do begin
  349.             GetAppFiles(i, tf);
  350.             if tf.fType <> myAppType then begin
  351.                 oe := GetDirID(tf.vRefNum, ovrn, odirID);
  352.                 AddFile(ovrn, odirID, tf.fName, prefs.create_dir_state <> CDS_Never, -1);
  353.                 ClrAppFiles(i);
  354.             end;
  355.         end;
  356.     end;
  357.  
  358. end.